//
//  SettingNewPasswordViewController.m
//  ALS3.0
//
//  Created by guomianxiang on 17/3/13.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "SettingNewPasswordViewController.h"
//#import "LGtools.h"
//#import "ALSBarButton.h"
#import "TouchIDManager.h"
//#import "ALSConstNSString.h"
#import "SVProgressHUD.h"
#import "LoginWindow.h"
#import "ALSHeader.h"

#define k_loginWindow [LoginWindow shareLoginWindw]

@interface SettingNewPasswordViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *NewPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *ascertainBut;
@property (weak, nonatomic) IBOutlet UILabel *theNewPresentationLab;
@property (weak, nonatomic) IBOutlet UIButton *showPasswordBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *theNewPresentationHeight;
//@property (weak, nonatomic) UITextField *didEditTextField;

@end


@implementation SettingNewPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupChildView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    UIButton *backButton = [ALSBarButton backButtonWithTitle:@"返回"];
    [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.title = @"设置新密码";
}

- (void)setupChildView {
    self.ascertainBut.layer.cornerRadius = 6;
    self.ascertainBut.layer.masksToBounds = YES;
    self.NewPasswordTextField.delegate = self;
    self.NewPasswordTextField.tintColor = kALSHightBlueColor;
    [self textFieldShouldEndEditing:self.NewPasswordTextField];
    self.theNewPresentationLab.hidden = YES;
    
    if (!_needValid) {
        [self.ascertainBut setTitle:@"确认" forState:(UIControlStateNormal)];
        
        UIButton *right = [ALSBarButton normalButtonWithNormalTitle:@"取消" selectedTitle:@"取消" highlightTitle:@"取消"];
        [right addTarget:k_loginWindow action:@selector(dismiss) forControlEvents:(UIControlEventTouchUpInside)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    }
    
}

- (IBAction)ascertainBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;

    if ([self checkingPasswordAndPresentation]) {
        
        if (!_needValid) {
            [weakSelf setNewPassword];

            [k_loginWindow showSetNewPasswordSuccessHUDAndDismiss];
            return;
        }
        
        TouchIDManager *IDManager = [TouchIDManager sharedTouchIDManager];
        
        [IDManager validateTouchIDWithLocalizedReason:@"验证已有指纹" localizedFallbackTitle:nil successHandler:^{
            
            [k_loginWindow showSetNewPasswordSuccessHUDAndDismiss];            
            
            [weakSelf setNewPassword];
            
        } failHandler:^(NSError * _Nullable error) {
            NSLog(@"error : %@",error.localizedDescription);
            switch (error.code) {
                case LAErrorTouchIDLockout:
                    [k_loginWindow showTouchIDLockHUD];
                    break;
                case LAErrorAuthenticationFailed:
                    [k_loginWindow showTouchIDAuthenticationFailedHUD];
                default:
                    break;
            }
        }];
    }
}

- (IBAction)showPasswordBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.NewPasswordTextField.secureTextEntry = NO;
    }else {
        self.NewPasswordTextField.secureTextEntry = YES;
    }
}

- (BOOL)checkingPasswordAndPresentation {
    
    NSString *newPassword = self.NewPasswordTextField.text;
    
    if (newPassword.length != 6) {
        self.theNewPresentationLab.hidden = NO;
        return NO;
    }
    
    NSScanner *scan = [NSScanner scannerWithString:newPassword];
    int val;
    if ([scan scanInt:&val] && [scan isAtEnd]) {
        self.theNewPresentationLab.hidden = YES;
        return YES;
    }
    
    self.theNewPresentationLab.hidden = NO;
    return NO;


//    NSString *newPassword = self.NewPasswordTextField.text;
//
//    //判断是否为中文
//    for (int i=0; i<newPassword.length; i++) {
//        NSRange range = NSMakeRange(i, 1);
//        NSString *subString = [newPassword substringWithRange:range];
//        const char *cChar = [subString UTF8String];
//        if (strlen(cChar)==3) {
//            self.theNewPresentationLab.hidden = NO;
//            self.theNewPresentationLab.text = @"密码不能为中文";
//            self.theNewPresentationHeight.constant = 25;
//            
//            [UIView animateWithDuration:0.2 animations:^{
//                [self.view layoutIfNeeded];
//            }];
//            
//            return NO;
//        }
//    }
//    
//    if (newPassword.length >= 6 && newPassword.length <= 16) {
//        self.theNewPresentationLab.hidden = YES;
//        self.theNewPresentationHeight.constant = 0;
//        return YES;
//    }else {
//        if (newPassword.length > 0) {
//            self.theNewPresentationLab.text = @"新密码长度为6-16个字符";
//            self.theNewPresentationLab.hidden = NO;
//            self.theNewPresentationHeight.constant = 25;
//        }else {
//            self.theNewPresentationLab.hidden = YES;
//            self.theNewPresentationHeight.constant = 0;
//        }
//    }
//    
//    [UIView animateWithDuration:0.2 animations:^{
//        [self.view layoutIfNeeded];
//    }];
//    
//    return NO;
}

- (void)setNewPassword {
    [NSUD setObject:self.NewPasswordTextField.text forKey:ALSPassword];
    
    if ([self.Delegete respondsToSelector:@selector(settingNewPasswordSuccess)]) {
        [self.Delegete settingNewPasswordSuccess];
    }
 
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor= kColorFromRGB(0x3fedeb).CGColor;
    textField.layer.borderWidth= 1.0f;
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    [self checkingPasswordAndPresentation];
    
    textField.layer.cornerRadius = 8.0f;
    textField.layer.masksToBounds = YES;
    textField.layer.borderColor = kColorFromRGB(0xa0a0a0).CGColor;
    textField.layer.borderWidth = 1.0f;
    return YES;
}

#pragma mark - load

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)setNeedValid:(BOOL)needValid {
    _needValid = needValid;
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
